#include "knightlore.h"
#include "konami_font.h"

static byte sin_lut[] PROGMEM = {
  0x00,0x03,0x06,0x09,0x0c,0x0f,0x12,0x15,0x19,0x1c,0x1f,0x22,0x25,0x28,0x2b,0x2e,
  0x31,0x34,0x36,0x39,0x3c,0x3f,0x42,0x44,0x47,0x49,0x4c,0x4f,0x51,0x53,0x56,0x58,
  0x5a,0x5c,0x5f,0x61,0x63,0x65,0x67,0x68,0x6a,0x6c,0x6e,0x6f,0x71,0x72,0x73,0x75,
  0x76,0x77,0x78,0x79,0x7a,0x7b,0x7c,0x7d,0x7d,0x7e,0x7e,0x7f,0x7f,0x7f,0x7f,0x7f,
  0x7f,0x7f,0x7f,0x7f,0x7f,0x7e,0x7e,0x7d,0x7d,0x7c,0x7b,0x7b,0x7a,0x79,0x78,0x77,
  0x75,0x74,0x73,0x71,0x70,0x6e,0x6d,0x6b,0x69,0x68,0x66,0x64,0x62,0x60,0x5e,0x5b,
  0x59,0x57,0x55,0x52,0x50,0x4d,0x4b,0x48,0x46,0x43,0x40,0x3d,0x3b,0x38,0x35,0x32,
  0x2f,0x2c,0x29,0x26,0x23,0x20,0x1d,0x1a,0x17,0x14,0x11,0x0e,0x0b,0x07,0x04,0x01,
  0xff,0xfc,0xf9,0xf5,0xf2,0xef,0xec,0xe9,0xe6,0xe3,0xe0,0xdd,0xda,0xd7,0xd4,0xd1,
  0xce,0xcb,0xc8,0xc5,0xc3,0xc0,0xbd,0xba,0xb8,0xb5,0xb3,0xb0,0xae,0xab,0xa9,0xa7,
  0xa5,0xa2,0xa0,0x9e,0x9c,0x9a,0x98,0x97,0x95,0x93,0x92,0x90,0x8f,0x8d,0x8c,0x8b,
  0x89,0x88,0x87,0x86,0x85,0x85,0x84,0x83,0x83,0x82,0x82,0x81,0x81,0x81,0x81,0x81,
  0x81,0x81,0x81,0x81,0x81,0x82,0x82,0x83,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8a,
  0x8b,0x8d,0x8e,0x8f,0x91,0x92,0x94,0x96,0x98,0x99,0x9b,0x9d,0x9f,0xa1,0xa4,0xa6,
  0xa8,0xaa,0xad,0xaf,0xb1,0xb4,0xb7,0xb9,0xbc,0xbe,0xc1,0xc4,0xc7,0xca,0xcc,0xcf,
  0xd2,0xd5,0xd8,0xdb,0xde,0xe1,0xe4,0xe7,0xeb,0xee,0xf1,0xf4,0xf7,0xfa,0xfd };

byte aa = 0;
byte z = 255;
byte zz = 2;
short g_time = 0;
byte aperture = 16;

char str[] = "HOLA, LETS GET SOME CLASSIC SCROLL... AND HOW ABOUT SOME NICE ZOOMING 1,2,3,4,5,6,7,8 ASDFGHJKLQWERTYUIOPZXCVBNM";
char str2[] = "WOOOOOW... WHAT IS THIS??? ARDUINO IS GOING TO EXPLODE!!!,3,4,5,6,7,8 ASDFGHJKLQWERTYUIOPZXCVBNM";

void InitRotoZoom()
{
  for( int i = 0; i < strlen( str ); i++ )
  {
    str[ i ] = ( str[ i ] - ' ' ) << 3;
  }

  for( int i = 0; i < strlen( str2 ); i++ )
  {
    str2[ i ] = ( str2[ i ] - ' ' ) << 3;
  }
}

byte ScrollText(char x, char y, int time)
{
  if ( y< 32 - 5 /*|| y>32+4+8 */)
    return 0;

  if ( y< 32 + 4 )
  {
    byte cc = (x + time) ;
    char c = str[ ( cc >> 3 ) & 127 ];
    if ( c == 32 )
      return 0;
  
    return ( pgm_read_byte( &konami_font[ c/*( ( c-' ' ) << 3 )*/ + ( cc & 7 ) ] ) & (byte)( 1 << ( y - 24  - 3 ) ) );
  }
  else if ( y< 32 + 4 + 8 )
  {
    byte cc = (x + time*2);
    //return ( sin_lut[ cc & 255] >>5 ) == ( y - ( 32 + 8 ) );
   
    char c = str2[ ( cc >> 3 ) & 127 ];
    if ( c == 32 )
      return 0;

    return ( pgm_read_byte( &konami_font[ c/*( ( c-' ' ) << 3 )*/ + ( cc & 7 ) ] ) & (byte)( 1 << ( y - 24  - 3 -8) ) );

  }
  else
    return 0;

}

void RotoZoom_Slow()
{
  aa++;

  short _sin = (short)pgm_read_byte( &sin_lut[aa & 255] );
  short _cos = (short)pgm_read_byte( &sin_lut[(aa + 64) & 255] );

  short x1 = (short)( -64 * _cos - 32 * _sin);
  short y1 = (short)( -64 * _sin + 32 * _cos);

  for(byte y=0;y<64;y++)
  {
    x1 += _sin;
    y1 -= _cos;

    short x2 = x1;
    short y2 = y1;

    for(byte x=0;x<128;x++)
    {
      x2 += _cos;
      y2 += _sin;

      char xx = (char)(x2 >> 8);
      char yy = (char)(y2 >> 8);

      if ( (((xx>>3)&1) ^ ((yy>>3)&1)) == 0)
      {
        KS108_SetPixel(y, x, SET );
      }
      else
      {
        KS108_SetPixel(y, x, CLEAR );
      }

    }
  }
}


byte data[128];

void ClearData()
{
  for(byte x=0;x<128;x++)
  {
    data[x] = 0;
  }
}


/*
byte GetStr( byte x, byte y )
{
 char str[] = {"RAUL"};
 
 const char *data = &font5x8[ ( str[x>>3] -' ') * 5];
 
 for(byte i=0;i<5;i++)
 {
 KS108_SendData( pgm_read_byte(data++) );
 }
}
*/

void RotoZoom()
{
  //aa+=1;

  short _sin = (short)pgm_read_byte( &sin_lut[aa & 255] );
  short _cos = (short)pgm_read_byte( &sin_lut[(aa + 64) & 255] );

  // zoom
/*
  z += zz;
  if (z > 200)
    zz = -1;
  if (z < 100)
    zz = 1;
*/
  _sin = (z* _sin)>>8;
  _cos = (z* _cos)>>8;

  short x1 = (short)( -64 * _cos - (32 - aperture ) * _sin);
  short y1 = (short)(  64 * _sin - (32 - aperture ) * _cos);

  byte cnt = 1;

  ClearData();

  byte x,y;
  byte xx,yy;

  for( y = aperture; y < 64 - aperture; y++)
  {
    short x2 = x1;
    short y2 = y1;


    for( x=0;x<128;x++)
    {
      xx = 64 + ((byte)(x2 >> 7)) & 127;
      yy = 32 + ((byte)(y2 >> 7)) & 63;

      /*
      if ( !(pgm_read_byte( &konami[ xx + (yy>>3)*128 ] ) & (1<<(yy&7))) )
      {
        data[x] |= cnt;
      }
      */
/*
       if ( ((xx ^ yy)>>2) &1 )
       {
         data[x] |= cnt;
       }
*/

      if ( ScrollText(xx,yy,g_time) )
        data[x] |= cnt;


      x2 += _cos;
      y2 -= _sin;      

    }

    x1 += _sin;
    y1 += _cos;


    cnt<<=1;

    if ( cnt == 0 )
    {
      KS108_Draw8bitPicRow( y>>3, data );
      ClearData();
      cnt = 1;
    }
  }

  g_time+=2;

}



